package net.shenru.encogs;

import java.util.ArrayList;
import java.util.List;

import net.shenru.os.task.ITask.TaskBundle;
import net.shenru.os.task.Task;
import net.shenru.os.task.TaskManager;
import net.shenru.os.utlis.SrLog;
import android.content.Context;

/**
 * 爬虫司令
 */
public class EncogChief implements EncogListener {

	private static final String TAG = EncogChief.class.getSimpleName();
	
	private static EncogChief instance;
	private EncogQueen queen;

	private TaskManager mTaskManager;
	
	private List<EncogTask> lists;

	private EncogChief() {
		queen = new EncogQueen();
		mTaskManager = new TaskManager();
		lists = new ArrayList<EncogTask>();
	}

	public static synchronized EncogChief getInstance(Context context) {
		if (instance == null) {
			instance = new EncogChief();
		}
		return instance;
	}

	public void setEncogTask(EncogTask task) {

	}

	public void run(EncogTask task) {
		lists.add(task);
		final EncogBus encog = (EncogBus) queen.getEncog(task.getType());
		encog.setTask(task);
		encog.setEncogListener(this);
		Task t = new Task(encog.getId());
		t.setTaskBundle(new TaskBundle() {
			@Override
			public void exece() {
				SrLog.i(TAG, "放出去一个爬虫");
				encog.run();
			}
		});
		mTaskManager.addTask(t);
	}

	//--------------------------------------
	@Override
	public void onEncogStart(Encog encog) {
		SrLog.i(TAG, "新加一个爬虫");
	}

	@Override
	public void onEncogEnd(Encog encog) {
		SrLog.i(TAG, "一个爬虫任务完毕");
	}

	@Override
	public void onHttpStart(EncogInfo info) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onHttpResult(EncogResult result) {
		// TODO Auto-generated method stub
		
	}

}
